package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.entities.BasePage;
import com.ttg.model.mapper.BankCardDataInfoMapper;
import com.ttg.model.pojo.BankCardBindingInfo;
import com.ttg.model.pojo.BankCardDataInfo;
import com.ttg.model.vo.VipBindAccountVO;
import com.ttg.web.dao.BankCardDataInfoDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangHongYuan
 * @description
 * @date 2024/4/16 16:44
 */
@Service
public class BankCardDataInfoDaoImpl extends ServiceImpl<BankCardDataInfoMapper, BankCardDataInfo> implements BankCardDataInfoDao {

    @Override
    public BasePage<VipBindAccountVO> getVipBindCardPage(BasePage<VipBindAccountVO> basePage, String vipId) {

        return baseMapper.getVipBindCardPage(basePage,vipId);
    }

    @Override
    public List<BankCardDataInfo> getByCstIdAndDateLast(String cstId) {
        LambdaQueryWrapper<BankCardDataInfo> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(BankCardDataInfo::getCstId,cstId);
        return list(queryWrapper);
    }

    @Override
    public BankCardDataInfo getIdByCardNo(String cardNo) {
        LambdaQueryWrapper<BankCardDataInfo> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.select(BankCardDataInfo::getId);
        queryWrapper.eq(BankCardDataInfo::getBankCardNumber,cardNo);
        return getOne(queryWrapper);
    }

    @Override
    public int countByCardNo(String cardNo) {
        LambdaQueryWrapper<BankCardDataInfo> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(BankCardDataInfo::getBankCardNumber, cardNo);
        return count(queryWrapper);
    }

    @Override
    public int countByCstId(String cstId) {
        LambdaQueryWrapper<BankCardDataInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankCardDataInfo::getCstId, cstId);
        return count(queryWrapper);
    }


    @Override
    public Map<String, Long> existsMapByBankCards(List<String> existsWhere) {
        LambdaQueryWrapper<BankCardDataInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(BankCardDataInfo::getBankCardNumber, existsWhere);
        Map<String, Long> rsMap = new HashMap<>();
        for (BankCardDataInfo bankCardDataInfo : list(lambdaQueryWrapper)) {
            rsMap.put(bankCardDataInfo.getBankCardNumber(), bankCardDataInfo.getId());
        }
        return rsMap;
    }

    @Override
    public List<Long> getVipIdsByTags(List<TagProducer> tags, String key) {
        return baseMapper.getVipIdsByTags(tags,key);
    }

    @Override
    public Map<String, Long> existsMap(List<Map<String, String>> existsWhere) {
        if (CollectionUtils.isEmpty(existsWhere)) {
            return Collections.emptyMap();
        }
        Map<String, Long> rsMap = new HashMap<>();
        for (BankCardDataInfo bankCardDataInfo : baseMapper.existsMap(existsWhere)) {
            rsMap.put(bankCardDataInfo.getCstId() + bankCardDataInfo.getBankCardNumber(), bankCardDataInfo.getId());
        }

        return rsMap;
    }

    @Override
    public BasePage<BankCardDataInfo> getVipBindAccountPage(BasePage<BankCardDataInfo> page, String vipId) {
        return baseMapper.getVipBindAccountPage(page,vipId);
    }
}
